package com.yuerlive.rainbow.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidadvance.topsnackbar.TSnackbar;
import com.umeng.analytics.MobclickAgent;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.utils.DeviceUtils;
import com.yuerlive.rainbow.utils.ToastUtils;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xiaoyi on 2017/5/17.
 */

public abstract class BaseFragment<T extends BasePresenter> extends Fragment {
    @Nullable
    @BindView(R.id.statusBar)
    View statusBar;
    @Nullable
    @BindView(R.id.headerBack)
    ImageView headBack;
    @Nullable
    @BindView(R.id.headerTitle)
    TextView headerTitle;

    public T presenter;
    protected String TAG = getClass().getSimpleName();

    protected abstract int getLayoutId();

    protected Activity mActivity;
    Unbinder unbinder;
    protected View root;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null)
            root = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, root);
        init();
        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initViews(root);
    }

    public abstract void initViews(View root);

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        releasePresenter();
        unbinder.unbind();
    }

    private void releasePresenter() {
        if (presenter != null) {
            presenter.unSubscribe();
        }
    }

    private void init() {
        setStatus();
        if (headBack != null)
            headBack.setOnClickListener(v -> mActivity.onBackPressed());
    }

    /*状态栏view*/
    private void setStatus() {
        if (statusBar == null || Build.VERSION.SDK_INT < 21) return;
        ViewGroup.LayoutParams params = statusBar.getLayoutParams();
        params.height = DeviceUtils.deviceStatusHeight(mActivity);
        statusBar.setLayoutParams(params);
        setTranslucentStatus(true);
    }

    protected void setTranslucentStatus(boolean on) {
        Window win = mActivity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    @UiThread
    protected void showToast(int resId) {
        ToastUtils.show(resId);
    }

    @UiThread
    protected void showToast(String msg) {
        ToastUtils.show(msg);
    }

    @UiThread
    protected void showToast(String msg, int duration) {
        ToastUtils.show(msg, duration);
    }

    @UiThread
    protected void showToast(int resId, int duration) {
        ToastUtils.show(resId, duration);
    }

    protected void jumpToActivity(@NonNull Class clazz) {
        jumpToActivity(clazz, null);
    }

    protected void jumpToActivity(@NonNull Class clazz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mActivity, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);

    }

    protected void jumpToActivity(@NonNull Intent intent) {
        startActivity(intent);

    }

    protected void jumpToActivityForResult(@Nullable Class clazz, int requestCode) {
        jumpToActivityForResult(clazz, requestCode, null);
    }

    protected void jumpToActivityForResult(@Nullable Class clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mActivity, clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);

    }

    @UiThread
    protected void showSnackbar(View view, String tips) {
        TSnackbar snackbar = TSnackbar
                .make(view, tips, TSnackbar.LENGTH_LONG);
        View snackbarView = snackbar.getView();
        snackbarView.setMinimumHeight(DeviceUtils.dp2px(mActivity,73));
        snackbarView.setBackgroundColor(mActivity.getResources().getColor(R.color.login_out_color));
        TextView tv = (TextView) snackbarView.findViewById(R.id.snackbar_text);
        tv.setTextColor(this.getResources().getColor(R.color.white));
        tv.setGravity(Gravity.CENTER);
        snackbar.show();
    }
    protected View getRootView(Activity context)
    {
        return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }
}
